
/**
 * name:        个人中心--我的收藏
 * author:      wuwq & lyj
 * Time:        18/01/26
 * Copyright:   bgemall.com
 */

$(function(){
    // 我的收藏
  
    
    if($.cookie('user')!=''&&$.cookie('user')!=null){
        var token='' 
        isLogin()    
        getToken();     // 获取/验证token 
        if( token!='' && token!=null){
            $('.member_favorites').append('<div class="none" style="width:100%;height:300px;background: #fff url(../../img/base/loading.gif) center center no-repeat;"></div>');
            getGoodsFavorites(token,URL);
        }
    }else{
        window.location.href='/src/login/login.html?type=2'
    }

    // 导航栏显示已登录
    function isLogin(){
        $('.yes_or_no').remove();
        $('.mod_login').addClass('mod_login2')
        var nav=''
        nav+='<span class="hello" style="margin-left:50px;">您好，</span><span class="nike_name"></span>'
        // nav+='<span class="avater"><img src="../images/base/member_msg_pic.png" alt=""></span>'
        nav+='<i class="icon_down"></i>'
        nav+='<ul class="logined">'
        nav+='<li class="member_center"><a href="../member/member_index.html"><i></i><span>会员中心</span></a></li>'
        nav+='<li class="login_out"><a href="javascript:;"><i></i><span>退出登录</span></a></li>'
        nav+='</ul>'
        $('.mod_login').append(nav);
    }

    // 获取/验证token
    function getToken(){
        strs=$.cookie('user').split(",");
        for(var i in strs){ 
            $('.nike_name').html(strs[1]) 
            token=strs[6];
            name=strs[1];
        }
    }
    // 退出登录
    $('.login_out').click(function(){
        $.cookie('user', null, { path: '/' });
            $.ajax({
                type:'post',
                url:new_url+'shop/login/logOut',
                dataType:'json',
                data:'',
                success:function(data){
                    tc(data.message)
                }  
            })
            setTimeout(function(){
                window.location.href=new_url+'index.html'
            },1100);
    });
});


// 
function getGoodsFavorites(token,URL){
    $.ajax({
        type:'post',
        url:new_url+'shop/member_favorites/getGoodsFavoritesList',
        dataType:'json',
        data:{token:token},  
        success:function(e){ 
            $('.none').remove();
            if(e.code=='0'){
                if(e.data.length=='0'){ 
                    $('.member_favorites ul').append('<div style="width:100%;height:200px;text-align:center;line-height:200px;color:#999;">您暂时还没有收藏商品！</div>');
                }else{
                    var favoritesTpl = $("#favoritesTpl").html();
                    var favoritesTplTwo = $("#favoritesTplTwo").html();
                    for(var i=0; i < e.data.length; i++){
                        
                            var favoritesCont=template(
                                favoritesTpl, {
                                    favoritesList: [{
                                        goodsCommonId:e.data[i].goodsCommonId,
                                        goodsId:e.data[i].goodsId,
                                        image:e.data[i].image,
                                        marketPrice:e.data[i].marketPrice,
                                        name:e.data[i].name,
                                        unitPrice:e.data[i].unitPrice,
                                        isDelete:e.data[i].isDelete,
                                        state:e.data[i].state
                                    }]
                                }
                            );
                            $('.member_favorites ul').append(favoritesCont);
                            var favorites_status=document.getElementsByClassName('favorites_status')
                            var goods_pic=document.getElementsByClassName('goods_pic')
                            // 是否下架
                            // if(e.data[i].state == '0'){
                            //     favorites_status[i].style="dispaly:block"
                            //     $('.favorites_status').text('已下架').css({
                            //         'text-align': 'center',
                            //         'line-height': '100px',
                            //         'font-size':'18px',
                            //         'color':'#fff',
                            //         'font-weight':'600'
                            //     })
                            // }
                            // // 是否商家商家删除
                            // if(e.data[i].isDelete=='1'){
                            //     favorites_status[i].style="dispaly:block"
                            //     // goods_pic[i].href='/index.html'
                            //     $('.favorites_status').text('商家已下架').css({
                            //         'text-align': 'center',
                            //         'line-height': '100px',
                            //         'font-size':'18px',
                            //         'color':'#fff',
                            //         'font-weight':'600'
                            //     })                               
                            // }
                            $('.member_favorites ul li').each(function(){
                                if($(this).attr('data-state')=='0'){
                                    $(this).find('.favorites_status').show().text('已下架').css({
                                        'text-align': 'center',
                                        'line-height': '100px',
                                        'font-size':'18px',
                                        'color':'#fff',
                                        'font-weight':'600',
                                    })
                                }
                                if($(this).attr('data-isDelete')=='1'){
                                    $(this).find('.favorites_status').show().text('已删除').css({
                                        'text-align': 'center',
                                        'line-height': '100px',
                                        'font-size':'18px',
                                        'color':'#fff',
                                        'font-weight':'600',
                                    })
                                }
                            })
                    }
                    $(".favorites_delete").click(function(){
                        var goodsCommonId = $(this).parent("li").attr("data-common-id"); 
                        $.ajax({
                            type:'post',
                            url:new_url+'shop/member_favorites/deleteGoodsFavorites',
                            dataType:'json',
                            data:{token:token,goodsCommonId:goodsCommonId},  
                            success:function(s){ 
                                if(s.code=='0'){
                                    tc(s.message);
                                    setTimeout(function(){
                                        window.location.reload();
                                    },1100)
                                }
                                else{
                                    tc(s.message);
                                }
                            }
                        })
                    });
                }
            }else{
                tc(e.message);
                setTimeout(function(){
                        window.location.href='/src/login/login.html?type=2'
                },1100)
            } 
        }
    });
}